package ltd.ninefish.framework.core.web.page;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import ltd.ninefish.framework.core.constant.DatabaseConst;

/**
 * @author zhaoxiubin
 * create-time 2023-12-16 14:40
 * description 分页信息
 **/

public class PageInfo {
    /**
     * 页码
     */
    private Integer pageNum;
    /**
     * 每页记录数
     */
    private Integer pageSize;
    /**
     * 总记录数
     */
    private Long total;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 排序字段
     */
    private String sortField;
    /**
     * 排序方式
     */
    private String sortBy = DatabaseConst.ASC;
    /**
     * 分页参数合理化
     */
    private Boolean reasonable = true;

    public String getOrderBy()
    {
        if (StrUtil.isEmpty(sortField))
        {
            return "";
        }
        return StrUtil.toUnderlineCase(sortField) + " " + sortBy;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        if (StrUtil.isNotEmpty(sortBy) && ArrayUtil.contains(DatabaseConst.SORT_BY_ARRAY, sortBy.toLowerCase()))
        {
            // 兼容前端排序类型
            if ("ascending".equals(sortBy))
            {
                sortBy = "asc";
            }
            else if ("descending".equals(sortBy))
            {
                sortBy = "desc";
            }
            this.sortBy = sortBy;
        }
    }

    public Boolean getReasonable() {
        if (ObjectUtil.isNull(reasonable))
        {
            return Boolean.TRUE;
        }
        return reasonable;
    }

    public void setReasonable(Boolean reasonable) {
        this.reasonable = reasonable;
    }
}
